home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / remote-vx.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  40KB  |  1,488 lines

  1. /* Memory-access and commands for remote VxWorks processes, for GDB.
  2.    Copyright (C) 1990-1991  Free Software Foundation, Inc.
  3.    Contributed by Wind River Systems and Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26. #include "wait.h"
  27. #include "target.h"
  28. #include "gdbcore.h"
  29. #include "command.h"
  30. #include "symtab.h"
  31. #include "symfile.h"        /* for struct complaint */
  32.  
  33. #include <string.h>
  34. #include <errno.h>
  35. #include <signal.h>
  36. #include <fcntl.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <sys/socket.h>
  40. #define free bogon_free        /* Sun claims "int free()" not void */
  41. #include <rpc/rpc.h>
  42. #undef free
  43. #include <sys/time.h>        /* UTek's <rpc/rpc.h> doesn't #incl this */
  44. #include <netdb.h>
  45. #include <ptrace.h>
  46. #include "xdr_ptrace.h"
  47. #include "xdr_ld.h"
  48. #include "xdr_rdb.h"
  49. #include "dbgRpcLib.h"
  50.  
  51. /* get rid of value.h if possible */
  52. #include <value.h>
  53. #include <symtab.h>
  54.  
  55. extern value call_function_by_hand ();
  56. extern void symbol_file_command ();
  57. extern int stop_soon_quietly;        /* for wait_for_inferior */
  58.  
  59. static int net_ptrace_clnt_call ();    /* Forward decl */
  60. static enum clnt_stat net_clnt_call ();    /* Forward decl */
  61. extern struct target_ops vx_ops, vx_run_ops;    /* Forward declaration */
  62.  
  63. /* Saved name of target host and called function for "info files".
  64.    Both malloc'd.  */
  65.  
  66. static char *vx_host;
  67. static char *vx_running;        /* Called function */
  68.  
  69. /* Nonzero means target that is being debugged remotely has a floating
  70.    point processor.  */
  71.  
  72. static int target_has_fp;
  73.  
  74. /* Default error message when the network is forking up.  */
  75.  
  76. static const char rpcerr[] = "network target debugging:  rpc error";
  77.  
  78. CLIENT *pClient;         /* client used in net debugging */
  79. static int ptraceSock = RPC_ANYSOCK;
  80.  
  81. enum clnt_stat net_clnt_call();
  82. static void parse_args ();
  83.  
  84. static struct timeval rpcTimeout = { 10, 0 };
  85.  
  86. static char *skip_white_space ();
  87. static char *find_white_space ();
  88.  
  89. /* Tell the VxWorks target system to download a file.
  90.    The load addresses of the text, data, and bss segments are
  91.    stored in pTextAddr, pDataAddr, and *pBssAddr (respectively).
  92.    Returns 0 for success, -1 for failure.  */
  93.  
  94. static int
  95. net_load (filename, pTextAddr, pDataAddr, pBssAddr)
  96.     char *filename;
  97.     CORE_ADDR *pTextAddr;
  98.     CORE_ADDR *pDataAddr;
  99.     CORE_ADDR *pBssAddr;
  100.     {
  101.     enum clnt_stat status;
  102.     struct ldfile ldstruct;
  103.     struct timeval load_timeout;
  104.  
  105.     bzero ((char *) &ldstruct, sizeof (ldstruct));
  106.  
  107.     /* We invoke clnt_call () here directly, instead of through
  108.        net_clnt_call (), because we need to set a large timeout value.
  109.        The load on the target side can take quite a while, easily
  110.        more than 10 seconds.  The user can kill this call by typing
  111.        CTRL-C if there really is a problem with the load.  
  112.        
  113.        Do not change the tv_sec value without checking -- select() imposes
  114.        a limit of 10**8 on it for no good reason that I can see...  */
  115.  
  116.     load_timeout.tv_sec = 99999999;   /* A large number, effectively inf. */
  117.     load_timeout.tv_usec = 0;
  118.  
  119.     status = clnt_call (pClient, VX_LOAD, xdr_wrapstring, &filename, xdr_ldfile,
  120.             &ldstruct, load_timeout);
  121.  
  122.     if (status == RPC_SUCCESS)
  123.       {
  124.         if (*ldstruct.name == NULL)    /* load failed on VxWorks side */
  125.           return -1;
  126.     *pTextAddr = ldstruct.txt_addr;
  127.     *pDataAddr = ldstruct.data_addr;
  128.     *pBssAddr = ldstruct.bss_addr;
  129.     return 0;
  130.       }
  131.     else
  132.         return -1;
  133.     }
  134.       
  135. /* returns 0 if successful, errno if RPC failed or VxWorks complains. */
  136.  
  137. static int
  138. net_break (addr, procnum)
  139.     int addr;
  140.     u_long procnum;
  141.     {
  142.     enum clnt_stat status;
  143.     int break_status;
  144.     Rptrace ptrace_in;  /* XXX This is stupid.  It doesn't need to be a ptrace
  145.                            structure.  How about something smaller? */
  146.  
  147.     bzero ((char *) &ptrace_in, sizeof (ptrace_in));
  148.     break_status = 0;
  149.  
  150.     ptrace_in.addr = addr;
  151.     ptrace_in.pid = inferior_pid;
  152.  
  153.     status = net_clnt_call (procnum, xdr_rptrace, &ptrace_in, xdr_int,
  154.                 &break_status);
  155.  
  156.     if (status != RPC_SUCCESS)
  157.     return errno;
  158.  
  159.     if (break_status == -1)
  160.       return ENOMEM;
  161.     return break_status;    /* probably (FIXME) zero */
  162.     }
  163.  
  164. /* returns 0 if successful, errno otherwise */
  165.  
  166. int
  167. vx_insert_breakpoint (addr)
  168.     int addr;
  169.     {
  170.     return net_break (addr, VX_BREAK_ADD);
  171.     }
  172.  
  173. /* returns 0 if successful, errno otherwise */
  174.  
  175. int
  176. vx_remove_breakpoint (addr)
  177.     int addr;
  178.     {
  179.     return net_break (addr, VX_BREAK_DELETE);
  180.     }
  181.  
  182. /* Start an inferior process and sets inferior_pid to its pid.
  183.    EXEC_FILE is the file to run.
  184.    ALLARGS is a string containing the arguments to the program.
  185.    ENV is the environment vector to pass.
  186.    Returns process id.  Errors reported with error().
  187.    On VxWorks, we ignore exec_file.  */
  188.  
  189. void
  190. vx_create_inferior (exec_file, args, env)
  191.      char *exec_file;
  192.      char *args;
  193.      char **env;
  194. {
  195.   enum clnt_stat status;
  196.   arg_array passArgs;
  197.   TASK_START taskStart;
  198.  
  199.   bzero ((char *) &passArgs, sizeof (passArgs));
  200.   bzero ((char *) &taskStart, sizeof (taskStart));
  201.  
  202.   /* parse arguments, put them in passArgs */
  203.  
  204.   parse_args (args, &passArgs);
  205.  
  206.   if (passArgs.arg_array_len == 0)
  207.     error ("You must specify a function name to run, and arguments if any");
  208.  
  209.   status = net_clnt_call (PROCESS_START, xdr_arg_array, &passArgs,
  210.               xdr_TASK_START, &taskStart);
  211.  
  212.   if ((status != RPC_SUCCESS) || (taskStart.status == -1))
  213.     error ("Can't create process on remote target machine");
  214.  
  215.   /* Save the name of the running function */
  216.   vx_running = savestring (passArgs.arg_array_val[0],
  217.                strlen (passArgs.arg_array_val[0]));
  218.  
  219. #ifdef CREATE_INFERIOR_HOOK
  220.   CREATE_INFERIOR_HOOK (pid);
  221. #endif  
  222.  
  223.   push_target (&vx_run_ops);
  224.   inferior_pid = taskStart.pid;
  225.  
  226. #if defined (START_INFERIOR_HOOK)
  227.   START_INFERIOR_HOOK ();
  228. #endif
  229.  
  230.   /* We will get a trace trap after one instruction.
  231.      Insert breakpoints and continue.  */
  232.  
  233.   init_wait_for_inferior ();
  234.  
  235.   /* Set up the "saved terminal modes" of the inferior
  236.      based on what modes we are starting it with.  */
  237.   target_terminal_init ();
  238.  
  239.   /* Install inferior's terminal modes.  */
  240.   target_terminal_inferior ();
  241.  
  242.   /* remote_start(args); */
  243.   /* trap_expected = 0; */
  244.   stop_soon_quietly = 1;
  245.   wait_for_inferior ();        /* Get the task spawn event */
  246.   stop_soon_quietly = 0;
  247.  
  248.   /* insert_step_breakpoint ();  FIXME, do we need this?  */
  249.   proceed(-1, -1, 0);
  250. }
  251.  
  252. /* Fill ARGSTRUCT in argc/argv form with the arguments from the
  253.    argument string ARGSTRING.  */
  254.  
  255. static void
  256. parse_args (arg_string, arg_struct)
  257.      register char *arg_string;
  258.      arg_array *arg_struct;
  259. {
  260.   register int arg_count = 0;    /* number of arguments */
  261.   register int arg_index = 0;
  262.   register char *p0;
  263.  
  264.   bzero ((char *) arg_struct, sizeof (arg_array));
  265.  
  266.   /* first count how many arguments there are */
  267.  
  268.   p0 = arg_string;
  269.   while (*p0 != '\0')
  270.     {
  271.       if (*(p0 = skip_white_space (p0)) == '\0')
  272.     break;
  273.       p0 = find_white_space (p0);
  274.       arg_count++;
  275.     }
  276.  
  277.   arg_struct->arg_array_len = arg_count;
  278.   arg_struct->arg_array_val = (char **) xmalloc ((arg_count + 1)
  279.                          * sizeof (char *));
  280.  
  281.   /* now copy argument strings into arg_struct.  */
  282.  
  283.   while (*(arg_string = skip_white_space (arg_string)))
  284.     {
  285.       p0 = find_white_space (arg_string);
  286.       arg_struct->arg_array_val[arg_index++] = savestring (arg_string,
  287.                                p0 - arg_string);
  288.       arg_string = p0;
  289.     }
  290.  
  291.   arg_struct->arg_array_val[arg_count] = NULL;
  292. }
  293.  
  294. /* Advance a string pointer across whitespace and return a pointer
  295.    to the first non-white character.  */
  296.  
  297. static char *
  298. skip_white_space (p)
  299.      register char *p;
  300. {
  301.   while (*p == ' ' || *p == '\t')
  302.     p++;
  303.   return p;
  304. }
  305.     
  306. /* Search for the first unquoted whitespace character in a string.
  307.    Returns a pointer to the character, or to the null terminator
  308.    if no whitespace is found.  */
  309.  
  310. static char *
  311. find_white_space (p)
  312.      register char *p;
  313. {
  314.   register int c;
  315.  
  316.   while ((c = *p) != ' ' && c != '\t' && c)
  317.     {
  318.       if (c == '\'' || c == '"')
  319.     {
  320.       while (*++p != c && *p)
  321.         {
  322.           if (*p == '\\')
  323.         p++;
  324.         }
  325.       if (!*p)
  326.         break;
  327.     }
  328.       p++;
  329.     }
  330.   return p;
  331. }
  332.     
  333. /* Poll the VxWorks target system for an event related
  334.    to the debugged task.
  335.    Returns -1 if remote wait failed, task status otherwise.  */
  336.  
  337. int
  338. net_wait (pEvent)
  339.     RDB_EVENT *pEvent;
  340. {
  341.     int pid;
  342.     enum clnt_stat status;
  343.  
  344.     bzero ((char *) pEvent, sizeof (RDB_EVENT));
  345.  
  346.     pid = inferior_pid;
  347.     status = net_clnt_call (PROCESS_WAIT, xdr_int, &pid, xdr_RDB_EVENT, pEvent);
  348.  
  349.     return (status == RPC_SUCCESS)? pEvent->status: -1;
  350. }
  351.     
  352. /* Suspend the remote task.
  353.    Returns -1 if suspend fails on target system, 0 otherwise.  */
  354.  
  355. int
  356. net_quit ()
  357. {
  358.     int pid;
  359.     int quit_status;
  360.     enum clnt_stat status;
  361.  
  362.     quit_status = 0;
  363.  
  364.     /* don't let rdbTask suspend itself by passing a pid of 0 */
  365.  
  366.     if ((pid = inferior_pid) == 0)
  367.     return -1;
  368.  
  369.     status = net_clnt_call (VX_TASK_SUSPEND, xdr_int, &pid, xdr_int,
  370.                 &quit_status);
  371.  
  372.     return (status == RPC_SUCCESS)? quit_status: -1;
  373. }
  374.  
  375. /* Read a register or registers from the remote system.  */
  376.  
  377. int
  378. vx_read_register (regno)
  379.      int regno;
  380. {
  381.   int status;
  382.   Rptrace ptrace_in;
  383.   Ptrace_return ptrace_out;
  384.   struct regs inferior_registers;
  385.   struct fp_status inferior_fp_registers;
  386.   extern char registers[];
  387.  
  388.   bzero ((char *) &ptrace_in, sizeof (ptrace_in));
  389.   bzero ((char *) &ptrace_out, sizeof (ptrace_out));
  390.  
  391.   /* FIXME, eventually only get the ones we need.  */
  392.   registers_fetched ();
  393.   
  394.   ptrace_in.pid = inferior_pid;
  395.   ptrace_out.info.more_data = (caddr_t) &inferior_registers;
  396.   status = net_ptrace_clnt_call (PTRACE_GETREGS, &ptrace_in, &ptrace_out);
  397.   if (status)
  398.     error (rpcerr);
  399.   if (ptrace_out.status == -1)
  400.     {
  401.       errno = ptrace_out.errno;
  402.       return -1;
  403.     }
  404.   
  405. #ifdef I80960
  406.  
  407.       bcopy ((char *) inferior_registers.r_lreg,
  408.          ®isters[REGISTER_BYTE (R0_REGNUM)], 16 * sizeof (int));
  409.       bcopy ((char *) inferior_registers.r_greg,
  410.          ®isters[REGISTER_BYTE (G0_REGNUM)], 16 * sizeof (int));
  411.  
  412.       /* Don't assume that a location in registers[] is properly aligned.  */
  413.  
  414.       bcopy ((char *) &inferior_registers.r_pcw,
  415.          ®isters[REGISTER_BYTE (PCW_REGNUM)], sizeof (int));
  416.       bcopy ((char *) &inferior_registers.r_acw,
  417.          ®isters[REGISTER_BYTE (ACW_REGNUM)], sizeof (int));
  418.       bcopy ((char *) &inferior_registers.r_lreg[2],    /* r2 (RIP) -> IP */
  419.          ®isters[REGISTER_BYTE (IP_REGNUM)], sizeof (int));
  420.       bcopy ((char *) &inferior_registers.r_tcw,
  421.          ®isters[REGISTER_BYTE (TCW_REGNUM)], sizeof (int));
  422.  
  423.       /* If the target has floating point registers, fetch them.
  424.      Otherwise, zero the floating point register values in
  425.      registers[] for good measure, even though we might not
  426.      need to.  */
  427.  
  428.       if (target_has_fp)
  429.     {
  430.       ptrace_in.pid = inferior_pid;
  431.       ptrace_out.info.more_data = (caddr_t) &inferior_fp_registers;
  432.       status = net_ptrace_clnt_call (PTRACE_GETFPREGS, &ptrace_in, &ptrace_out);
  433.       if (status)
  434.         error (rpcerr);
  435.       if (ptrace_out.status == -1)
  436.         {
  437.           errno = ptrace_out.errno;
  438.           return -1;
  439.         }
  440.       
  441.       bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  442.          REGISTER_RAW_SIZE (FP0_REGNUM) * 4);
  443.     }
  444.       else
  445.     {
  446.       bzero ((char *) ®isters[REGISTER_BYTE (FP0_REGNUM)],
  447.          REGISTER_RAW_SIZE (FP0_REGNUM) * 4);
  448.     }
  449.  
  450. #else  /* not 960, thus must be 68000:  FIXME!  */
  451.  
  452.   bcopy (&inferior_registers, registers, 16 * 4);
  453.   *(int *)®isters[REGISTER_BYTE (PS_REGNUM)] = inferior_registers.r_ps;
  454.   *(int *)®isters[REGISTER_BYTE (PC_REGNUM)] = inferior_registers.r_pc;
  455.  
  456.   if (target_has_fp)
  457.     {
  458.       ptrace_in.pid = inferior_pid;
  459.       ptrace_out.info.more_data = (caddr_t) &inferior_fp_registers;
  460.       status = net_ptrace_clnt_call (PTRACE_GETFPREGS, &ptrace_in, &ptrace_out);
  461.       if (status)
  462.     error (rpcerr);
  463.       if (ptrace_out.status == -1)
  464.     {
  465.       errno = ptrace_out.errno;
  466.       return -1;
  467.     }
  468.       
  469.       bcopy (&inferior_fp_registers, ®isters[REGISTER_BYTE (FP0_REGNUM)],
  470.          sizeof inferior_fp_registers.fps_regs);
  471.       bcopy (&inferior_fp_registers.fps_control,
  472.      ®isters[REGISTER_BYTE (FPC_REGNUM)],
  473.      sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  474.     }
  475.   else
  476.     {
  477.       bzero (®isters[REGISTER_BYTE (FP0_REGNUM)],
  478.          sizeof inferior_fp_registers.fps_regs);
  479.       bzero (®isters[REGISTER_BYTE (FPC_REGNUM)],
  480.      sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  481.     }
  482. #endif  /* various architectures */
  483.  
  484.   return 0;
  485. }
  486.  
  487. /* Prepare to store registers.  Since we will store all of them,
  488.    read out their current values now.  */
  489.  
  490. void
  491. vx_prepare_to_store ()
  492. {
  493.   vx_read_register (-1);
  494. }
  495.  
  496.  
  497. /* Store our register values back into the inferior.
  498.    If REGNO is -1, do this for all registers.
  499.    Otherwise, REGNO specifies which register (so we can save time).  */
  500.    /* FIXME, look at REGNO to save time here */
  501.  
  502. vx_write_register (regno)
  503.      int regno;
  504. {
  505.   struct regs inferior_registers;
  506.   struct fp_status inferior_fp_registers;
  507.   extern char registers[];
  508.   int status;
  509.   Rptrace ptrace_in;
  510.   Ptrace_return ptrace_out;
  511.  
  512.   bzero ((char *) &ptrace_in, sizeof (ptrace_in));
  513.   bzero ((char *) &ptrace_out, sizeof (ptrace_out));
  514.  
  515. #ifdef I80960
  516.  
  517.   bcopy (®isters[REGISTER_BYTE (R0_REGNUM)],
  518.      (char *) inferior_registers.r_lreg, 16 * sizeof (int));
  519.   bcopy (®isters[REGISTER_BYTE (G0_REGNUM)],
  520.      (char *) inferior_registers.r_greg, 16 * sizeof (int));
  521.  
  522.   /* Don't assume that a location in registers[] is properly aligned.  */
  523.  
  524.   bcopy (®isters[REGISTER_BYTE (PCW_REGNUM)],
  525.      (char *) &inferior_registers.r_pcw, sizeof (int));
  526.   bcopy (®isters[REGISTER_BYTE (ACW_REGNUM)],
  527.      (char *) &inferior_registers.r_acw, sizeof (int));
  528.   bcopy (®isters[REGISTER_BYTE (TCW_REGNUM)],
  529.      (char *) &inferior_registers.r_tcw, sizeof (int));
  530.  
  531. #else  /* not 960 -- assume 68k -- FIXME */
  532.  
  533.   bcopy (registers, &inferior_registers, 16 * 4);
  534.   inferior_registers.r_ps = *(int *)®isters[REGISTER_BYTE (PS_REGNUM)];
  535.   inferior_registers.r_pc = *(int *)®isters[REGISTER_BYTE (PC_REGNUM)];
  536.  
  537. #endif  /* Different register sets */
  538.  
  539.   ptrace_in.pid = inferior_pid;
  540.   ptrace_in.info.ttype     = REGS;
  541.   ptrace_in.info.more_data = (caddr_t) &inferior_registers;
  542.  
  543.   /* XXX change second param to be a proc number */
  544.   status = net_ptrace_clnt_call (PTRACE_SETREGS, &ptrace_in, &ptrace_out);
  545.   if (status)
  546.       error (rpcerr);
  547.   if (ptrace_out.status == -1)
  548.     {
  549.       errno = ptrace_out.errno;
  550.       return -1;
  551.     }
  552.  
  553.   /* Store floating point registers if the target has them.  */
  554.  
  555.   if (target_has_fp)
  556.     {
  557. #ifdef I80960
  558.  
  559.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
  560.          sizeof inferior_fp_registers.fps_regs);
  561.  
  562. #else  /* not 960 -- assume 68k -- FIXME */
  563.  
  564.       bcopy (®isters[REGISTER_BYTE (FP0_REGNUM)], &inferior_fp_registers,
  565.          sizeof inferior_fp_registers.fps_regs);
  566.       bcopy (®isters[REGISTER_BYTE (FPC_REGNUM)],
  567.      &inferior_fp_registers.fps_control,
  568.          sizeof inferior_fp_registers - sizeof inferior_fp_registers.fps_regs);
  569.  
  570. #endif  /* Different register sets */
  571.  
  572.       ptrace_in.pid = inferior_pid;
  573.       ptrace_in.info.ttype     = FPREGS;
  574.       ptrace_in.info.more_data = (caddr_t) &inferior_fp_registers;
  575.  
  576.       status = net_ptrace_clnt_call (PTRACE_SETFPREGS, &ptrace_in, &ptrace_out);
  577.       if (status)
  578.       error (rpcerr);
  579.       if (ptrace_out.status == -1)
  580.     {
  581.       errno = ptrace_out.errno;
  582.       return -1;
  583.     }
  584.     }
  585.   return 0;
  586. }
  587.  
  588. /* Copy LEN bytes to or from remote inferior's memory starting at MEMADDR
  589.    to debugger memory starting at MYADDR.  WRITE is true if writing to the
  590.    inferior.
  591.    Result is the number of bytes written or read (zero if error).  The
  592.    protocol allows us to return a negative count, indicating that we can't
  593.    handle the current address but can handle one N bytes further, but
  594.    vxworks doesn't give us that information.  */
  595.  
  596. int
  597. vx_xfer_memory (memaddr, myaddr, len, write, target)
  598.      CORE_ADDR memaddr;
  599.      char *myaddr;
  600.      int len;
  601.      int write;
  602.      struct target_ops *target;            /* ignored */
  603. {
  604.   int status;
  605.   Rptrace ptrace_in;
  606.   Ptrace_return ptrace_out;
  607.   C_bytes data;
  608.  
  609.   bzero ((char *) &ptrace_in, sizeof (ptrace_in));
  610.   bzero ((char *) &ptrace_out, sizeof (ptrace_out));
  611.  
  612.   ptrace_in.pid = inferior_pid;        /* XXX pid unnecessary for READDATA */
  613.   ptrace_in.addr = (int) memaddr;    /* Where from */
  614.   ptrace_in.data = len;            /* How many bytes */
  615.  
  616.   if (write)
  617.     {
  618.       ptrace_in.info.ttype     = DATA;
  619.       ptrace_in.info.more_data = (caddr_t) &data;
  620.  
  621.       data.bytes = (caddr_t) myaddr;    /* Where from */
  622.       data.len   = len;            /* How many bytes (again, for XDR) */
  623.  
  624.       /* XXX change second param to be a proc number */
  625.       status = net_ptrace_clnt_call (PTRACE_WRITEDATA, &ptrace_in, &ptrace_out);
  626.     }
  627.   else
  628.     {
  629.       ptrace_out.info.more_data = (caddr_t) &data;
  630.       data.bytes = myaddr;        /* Where to */
  631.       data.len   = len;            /* How many (again, for XDR) */
  632.  
  633.       /* XXX change second param to be a proc number */
  634.       status = net_ptrace_clnt_call (PTRACE_READDATA, &ptrace_in, &ptrace_out);
  635.     }
  636.  
  637.   if (status)
  638.       error (rpcerr);
  639.   if (ptrace_out.status == -1)
  640.     {
  641.       return 0;        /* No bytes moved */
  642.     }
  643.   return len;        /* Moved *all* the bytes */
  644. }
  645.  
  646. void
  647. vx_files_info ()
  648. {
  649.   printf ("\tAttached to host `%s'", vx_host);
  650.   printf (", which has %sfloating point", target_has_fp? "": "no ");
  651.   printf (".\n");
  652. }
  653.  
  654. void
  655. vx_run_files_info ()
  656. {
  657.   printf ("\tRunning %s VxWorks process %s", 
  658.       vx_running? "child": "attached",
  659.       local_hex_string(inferior_pid));
  660.   if (vx_running)
  661.     printf (", function `%s'", vx_running);
  662.   printf(".\n");
  663. }
  664.  
  665. void
  666. vx_resume (step, siggnal)
  667.      int step;
  668.      int siggnal;
  669. {
  670.   int status;
  671.   Rptrace ptrace_in;
  672.   Ptrace_return ptrace_out;
  673.  
  674.   if (siggnal != 0 && siggnal != stop_signal)
  675.     error ("Cannot send signals to VxWorks processes");
  676.  
  677.   bzero ((char *) &ptrace_in, sizeof (ptrace_in));
  678.   bzero ((char *) &ptrace_out, sizeof (ptrace_out));
  679.  
  680.   ptrace_in.pid = inferior_pid;
  681.   ptrace_in.addr = 1;    /* Target side insists on this, or it panics.  */
  682.  
  683.   /* XXX change second param to be a proc number */
  684.   status = net_ptrace_clnt_call (step? PTRACE_SINGLESTEP: PTRACE_CONT,
  685.                  &ptrace_in, &ptrace_out);
  686.   if (status)
  687.       error (rpcerr);
  688.   if (ptrace_out.status == -1)
  689.     {
  690.       errno = ptrace_out.errno;
  691.       perror_with_name ("Resuming remote process");
  692.     }
  693. }
  694.  
  695. void
  696. vx_mourn_inferior ()
  697. {
  698.   pop_target ();        /* Pop back to no-child state */
  699.   generic_mourn_inferior ();
  700. }
  701.  
  702.  
  703. /* This function allows the addition of incrementally linked object files.  */
  704.  
  705. void
  706. vx_load_command (arg_string, from_tty)
  707.      char* arg_string;
  708.      int from_tty;
  709. {
  710.   CORE_ADDR text_addr;
  711.   CORE_ADDR data_addr;
  712.   CORE_ADDR bss_addr;
  713.   
  714.   if (arg_string == 0)
  715.     error ("The load command takes a file name");
  716.  
  717.   arg_string = tilde_expand (arg_string);
  718.   make_cleanup (free, arg_string);
  719.  
  720.   dont_repeat ();
  721.  
  722.   QUIT;
  723.   immediate_quit++;
  724.   if (net_load (arg_string, &text_addr, &data_addr, &bss_addr) == -1)
  725.     error ("Load failed on target machine");
  726.   immediate_quit--;
  727.  
  728.   /* FIXME, for now we ignore data_addr and bss_addr.  */
  729.   symbol_file_add (arg_string, from_tty, text_addr, 0);
  730. }
  731.  
  732. #ifdef FIXME  /* Not ready for prime time */
  733. /* Single step the target program at the source or machine level.
  734.    Takes an error exit if rpc fails.
  735.    Returns -1 if remote single-step operation fails, else 0.  */
  736.  
  737. static int
  738. net_step ()
  739. {
  740.   enum clnt_stat status;
  741.   int step_status;
  742.   SOURCE_STEP source_step;
  743.  
  744.   source_step.taskId = inferior_pid;
  745.  
  746.   if (step_range_end)
  747.     {
  748.       source_step.startAddr = step_range_start;
  749.       source_step.endAddr = step_range_end;
  750.     }
  751.   else
  752.     {
  753.       source_step.startAddr = 0;
  754.       source_step.endAddr = 0;
  755.     }
  756.  
  757.   status = net_clnt_call (VX_SOURCE_STEP, xdr_SOURCE_STEP, &source_step,
  758.               xdr_int, &step_status);
  759.  
  760.   if (status == RPC_SUCCESS)
  761.     return step_status;
  762.   else 
  763.     error (rpcerr);
  764. }
  765. #endif
  766.  
  767. /* Emulate ptrace using RPC calls to the VxWorks target system.
  768.    Returns nonzero (-1) if RPC status to VxWorks is bad, 0 otherwise.  */
  769.  
  770. static int
  771. net_ptrace_clnt_call (request, pPtraceIn, pPtraceOut)
  772.     enum ptracereq request;
  773.     Rptrace *pPtraceIn;
  774.     Ptrace_return *pPtraceOut;
  775. {
  776.   enum clnt_stat status;
  777.  
  778.   status = net_clnt_call (request, xdr_rptrace, pPtraceIn, xdr_ptrace_return,
  779.               pPtraceOut);
  780.  
  781.   if (status != RPC_SUCCESS)
  782.       return -1;
  783.  
  784.   return 0;
  785. }
  786.  
  787. /* Query the target for the name of the file from which VxWorks was
  788.    booted.  pBootFile is the address of a pointer to the buffer to
  789.    receive the file name; if the pointer pointed to by pBootFile is 
  790.    NULL, memory for the buffer will be allocated by XDR.
  791.    Returns -1 if rpc failed, 0 otherwise.  */
  792.  
  793. int
  794. net_get_boot_file (pBootFile)
  795.      char **pBootFile;
  796. {
  797.   enum clnt_stat status;
  798.  
  799.   status = net_clnt_call (VX_BOOT_FILE_INQ, xdr_void, (char *) 0,
  800.               xdr_wrapstring, pBootFile);
  801.   return (status == RPC_SUCCESS) ? 0 : -1;
  802. }
  803.  
  804. /* Fetch a list of loaded object modules from the VxWorks target.
  805.    Returns -1 if rpc failed, 0 otherwise
  806.    There's no way to check if the returned loadTable is correct.
  807.    VxWorks doesn't check it.  */
  808.  
  809. int
  810. net_get_symbols (pLoadTable)
  811.      ldtabl *pLoadTable;        /* return pointer to ldtabl here */
  812. {
  813.   enum clnt_stat status;
  814.  
  815.   bzero ((char *) pLoadTable, sizeof (struct ldtabl));
  816.  
  817.   status = net_clnt_call (VX_STATE_INQ, xdr_void, 0, xdr_ldtabl, pLoadTable);
  818.   return (status == RPC_SUCCESS) ? 0 : -1;
  819. }
  820.  
  821. /* Look up a symbol in the VxWorks target's symbol table.
  822.    Returns status of symbol read on target side (0=success, -1=fail)
  823.    Returns -1 and complain()s if rpc fails.  */
  824.  
  825. struct complaint cant_contact_target =
  826.   {"Lost contact with VxWorks target", 0, 0};
  827.  
  828. int
  829. vx_lookup_symbol (name, pAddr)
  830.      char *name;        /* symbol name */
  831.      CORE_ADDR *pAddr;
  832. {
  833.   enum clnt_stat status;
  834.   SYMBOL_ADDR symbolAddr;
  835.  
  836.   *pAddr = 0;
  837.   bzero ((char *) &symbolAddr, sizeof (symbolAddr));
  838.  
  839.   status = net_clnt_call (VX_SYMBOL_INQ, xdr_wrapstring, &name,
  840.               xdr_SYMBOL_ADDR, &symbolAddr);
  841.   if (status != RPC_SUCCESS) {
  842.       complain (&cant_contact_target, 0);
  843.       return -1;
  844.   }
  845.  
  846.   *pAddr = symbolAddr.addr;
  847.   return symbolAddr.status;
  848. }
  849.  
  850. /* Check to see if the VxWorks target has a floating point coprocessor.
  851.    Returns 1 if target has floating point processor, 0 otherwise.
  852.    Calls error() if rpc fails.  */
  853.  
  854. int
  855. net_check_for_fp ()
  856. {
  857.   enum clnt_stat status;
  858.   bool_t fp = 0;    /* true if fp processor is present on target board */
  859.  
  860.   status = net_clnt_call (VX_FP_INQUIRE, xdr_void, 0, xdr_bool, &fp);
  861.   if (status != RPC_SUCCESS)
  862.       error (rpcerr);
  863.  
  864.    return (int) fp;
  865. }
  866.  
  867. /* Establish an RPC connection with the VxWorks target system.
  868.    Calls error () if unable to establish connection.  */
  869.  
  870. void
  871. net_connect (host)
  872.      char *host;
  873. {
  874.   struct sockaddr_in destAddr;
  875.   struct hostent *destHost;
  876.  
  877.   /* get the internet address for the given host */
  878.  
  879.   if ((destHost = (struct hostent *) gethostbyname (host)) == NULL)
  880.       error ("Invalid hostname.  Couldn't find remote host address.");
  881.  
  882.   bzero (&destAddr, sizeof (destAddr));
  883.  
  884.   destAddr.sin_addr.s_addr = * (u_long *) destHost->h_addr;
  885.   destAddr.sin_family      = AF_INET;
  886.   destAddr.sin_port        = 0;    /* set to actual port that remote
  887.                        ptrace is listening on.  */
  888.  
  889.   /* Create a tcp client transport on which to issue
  890.      calls to the remote ptrace server.  */
  891.  
  892.   ptraceSock = RPC_ANYSOCK;
  893.   pClient = clnttcp_create (&destAddr, RDBPROG, RDBVERS, &ptraceSock, 0, 0);
  894.   /* FIXME, here is where we deal with different version numbers of the proto */
  895.   
  896.   if (pClient == NULL)
  897.     {
  898.       clnt_pcreateerror ("\tnet_connect");
  899.       error ("Couldn't connect to remote target.");
  900.     }
  901. }
  902.  
  903. /* Sleep for the specified number of milliseconds 
  904.  * (assumed to be less than 1000).
  905.  * If select () is interrupted, returns immediately;
  906.  * takes an error exit if select () fails for some other reason.
  907.  */
  908.  
  909. static void
  910. sleep_ms (ms)
  911.      long ms;
  912. {
  913.   struct timeval select_timeout;
  914.   int status;
  915.  
  916.   select_timeout.tv_sec = 0;
  917.   select_timeout.tv_usec = ms * 1000;
  918.  
  919.   status = select (0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &select_timeout);
  920.  
  921.   if (status < 0 && errno != EINTR)
  922.     perror_with_name ("select");
  923. }
  924.  
  925. /* Wait for control to return from inferior to debugger.
  926.    If inferior gets a signal, we may decide to start it up again
  927.    instead of returning.  That is why there is a loop in this function.
  928.    When this function actually returns it means the inferior
  929.    should be left stopped and GDB should read more commands.  */
  930.  
  931. /* For network debugging with VxWorks.
  932.  * VxWorks knows when tasks hit breakpoints, receive signals, exit, etc,
  933.  * so vx_wait() receives this information directly from
  934.  * VxWorks instead of trying to figure out what happenned via a wait() call.
  935.  */
  936.  
  937. static int
  938. vx_wait (status)
  939.      int *status;
  940. {
  941.   register int pid;
  942.   WAITTYPE w;
  943.   RDB_EVENT rdbEvent;
  944.   int quit_failed;
  945.  
  946.   do
  947.     {
  948.       /* If CTRL-C is hit during this loop,
  949.      suspend the inferior process.  */
  950.  
  951.       quit_failed = 0;
  952.       if (quit_flag)
  953.     {
  954.       quit_failed = (net_quit () == -1);
  955.       quit_flag = 0;
  956.     }
  957.  
  958.       /* If a net_quit () or net_wait () call has failed,
  959.      allow the user to break the connection with the target.
  960.      We can't simply error () out of this loop, since the 
  961.      data structures representing the state of the inferior
  962.      are in an inconsistent state.  */
  963.  
  964.       if (quit_failed || net_wait (&rdbEvent) == -1)
  965.     {
  966.       terminal_ours ();
  967.       if (query ("Can't %s.  Disconnect from target system? ",
  968.              (quit_failed) ? "suspend remote task"
  969.                            : "get status of remote task"))
  970.         {
  971.           target_mourn_inferior();
  972.           error ("Use the \"target\" command to reconnect.");
  973.         }
  974.       else
  975.         {
  976.           terminal_inferior ();
  977.           continue;
  978.         }
  979.     }
  980.       
  981.       pid = rdbEvent.taskId;
  982.       if (pid == 0)
  983.     {
  984.       sleep_ms (200);    /* FIXME Don't kill the network too badly */
  985.     }
  986.       else if (pid != inferior_pid)
  987.     fatal ("Bad pid for debugged task: %s\n", local_hex_string(pid));
  988.     } while (pid == 0);
  989.  
  990.   /* FIXME, eventually do more then SIGTRAP on everything...  */
  991.   switch (rdbEvent.eventType)
  992.     {
  993.     case EVENT_EXIT:
  994.       WSETEXIT (w, 0);
  995.       /* FIXME is it possible to distinguish between a
  996.      XXX   normal vs abnormal exit in VxWorks? */
  997.       break;
  998.  
  999.     case EVENT_START:        /* Task was just started. */
  1000.       WSETSTOP (w, SIGTRAP);
  1001.       break;
  1002.  
  1003.     case EVENT_STOP:
  1004.       WSETSTOP (w, SIGTRAP);
  1005.       /* XXX was it stopped by a signal?  act accordingly */
  1006.       break;
  1007.  
  1008.     case EVENT_BREAK:        /* Breakpoint was hit. */
  1009.       WSETSTOP (w, SIGTRAP);
  1010.       break;
  1011.  
  1012.     case EVENT_SUSPEND:        /* Task was suspended, probably by ^C. */
  1013.       WSETSTOP (w, SIGINT);
  1014.       break;
  1015.  
  1016.     case EVENT_BUS_ERR:        /* Task made evil nasty reference. */
  1017.       WSETSTOP (w, SIGBUS);
  1018.       break;
  1019.  
  1020.     case EVENT_ZERO_DIV:    /* Division by zero */
  1021.       WSETSTOP (w, SIGFPE);    /* Like Unix, call it a float exception. */
  1022.  
  1023.     case EVENT_SIGNAL:
  1024.       /* The target is not running Unix, and its
  1025.      faults/traces do not map nicely into Unix signals.
  1026.      Make sure they do not get confused with Unix signals
  1027.      by numbering them with values higher than the highest
  1028.      legal Unix signal.  code in the arch-dependent PRINT_RANDOM_SIGNAL
  1029.      routine will interpret the value for wait_for_inferior.  */
  1030.       WSETSTOP (w, rdbEvent.sigType + NSIG);
  1031.       break;
  1032.     } /* switch */
  1033.   *status = *(int *)&w;        /* Grumble union wait crap Grumble */
  1034.   return pid;
  1035. }
  1036.  
  1037. static int
  1038. symbol_stub (arg)
  1039.      char *arg;
  1040. {
  1041.   symbol_file_command (arg, 0);
  1042.   return 1;
  1043. }
  1044.  
  1045. static int
  1046. add_symbol_stub (arg)
  1047.      char *arg;
  1048. {
  1049.   struct ldfile *pLoadFile = (struct ldfile *)arg;
  1050.  
  1051.   printf("\t%s: ", pLoadFile->name);
  1052.   symbol_file_add (pLoadFile->name, 0, pLoadFile->txt_addr, 0);
  1053.   printf ("ok\n");
  1054.   return 1;
  1055. }
  1056. /* Target command for VxWorks target systems.
  1057.  
  1058.    Used in vxgdb.  Takes the name of a remote target machine
  1059.    running vxWorks and connects to it to initialize remote network
  1060.    debugging.  */
  1061.  
  1062. static void
  1063. vx_open (args, from_tty)
  1064.      char *args;
  1065.      int from_tty;
  1066. {
  1067.   extern int close ();
  1068.   char *bootFile;
  1069.   extern char *source_path;
  1070.   struct ldtabl loadTable;
  1071.   struct ldfile *pLoadFile;
  1072.   int i;
  1073.   extern CLIENT *pClient;
  1074.  
  1075.   if (!args)
  1076.     error_no_arg ("target machine name");
  1077.  
  1078.   target_preopen (from_tty);
  1079.   
  1080.   unpush_target (&vx_ops);
  1081.   printf ("Attaching remote machine across net...\n");
  1082.   fflush (stdout);
  1083.  
  1084.   /* Allow the user to kill the connect attempt by typing ^C.
  1085.      Wait until the call to target_has_fp () completes before
  1086.      disallowing an immediate quit, since even if net_connect ()
  1087.      is successful, the remote debug server might be hung.  */
  1088.  
  1089.   immediate_quit++;
  1090.  
  1091.   net_connect (args);
  1092.   target_has_fp = net_check_for_fp ();
  1093.   printf_filtered ("Connected to %s.\n", args);
  1094.  
  1095.   immediate_quit--;
  1096.  
  1097.   push_target (&vx_ops);
  1098.  
  1099.   /* Save a copy of the target host's name.  */
  1100.   vx_host = savestring (args, strlen (args));
  1101.  
  1102.   /* Find out the name of the file from which the target was booted
  1103.      and load its symbol table.  */
  1104.  
  1105.   printf_filtered ("Looking in Unix path for all loaded modules:\n");
  1106.   bootFile = NULL;
  1107.   if (!net_get_boot_file (&bootFile))
  1108.     {
  1109.       if (*bootFile) {
  1110.     printf_filtered ("\t%s: ", bootFile);
  1111.     if (catch_errors (symbol_stub, bootFile,
  1112.         "Error while reading symbols from boot file:\n"))
  1113.       puts_filtered ("ok\n");
  1114.       } else if (from_tty)
  1115.     printf ("VxWorks kernel symbols not loaded.\n");
  1116.     }
  1117.   else
  1118.     error ("Can't retrieve boot file name from target machine.");
  1119.  
  1120.   clnt_freeres (pClient, xdr_wrapstring, &bootFile);
  1121.  
  1122.   if (net_get_symbols (&loadTable) != 0)
  1123.     error ("Can't read loaded modules from target machine");
  1124.  
  1125.   i = 0-1;
  1126.   while (++i < loadTable.tbl_size)
  1127.     {
  1128.       QUIT;    /* FIXME, avoids clnt_freeres below:  mem leak */
  1129.       pLoadFile = &loadTable.tbl_ent [i];
  1130. #ifdef WRS_ORIG
  1131.   {
  1132.     register int desc;
  1133.     struct cleanup *old_chain;
  1134.     char *fullname = NULL;
  1135.  
  1136.     desc = openp (source_path, 0, pLoadFile->name, O_RDONLY, 0, &fullname);
  1137.     if (desc < 0)
  1138.     perror_with_name (pLoadFile->name);
  1139.     old_chain = make_cleanup (close, desc);
  1140.     add_file_at_addr (fullname, desc, pLoadFile->txt_addr, pLoadFile->data_addr,
  1141.               pLoadFile->bss_addr);
  1142.     do_cleanups (old_chain);
  1143.   }
  1144. #else
  1145.       /* Botches, FIXME:
  1146.      (1)  Searches the PATH, not the source path.
  1147.      (2)  data and bss are assumed to be at the usual offsets from text.  */
  1148.       catch_errors (add_symbol_stub, (char *)pLoadFile, (char *)0);
  1149. #endif
  1150.     }
  1151.   printf_filtered ("Done.\n");
  1152.  
  1153.   clnt_freeres (pClient, xdr_ldtabl, &loadTable);
  1154. }
  1155.  
  1156. /* attach_command --
  1157.    takes a task started up outside of gdb and ``attaches'' to it.
  1158.    This stops it cold in its tracks and allows us to start tracing it.  */
  1159.  
  1160. static void
  1161. vx_attach (args, from_tty)
  1162.      char *args;
  1163.      int from_tty;
  1164. {
  1165.   int pid;
  1166.   char *cptr = 0;
  1167.   Rptrace ptrace_in;
  1168.   Ptrace_return ptrace_out;
  1169.   int status;
  1170.  
  1171.   dont_repeat();
  1172.  
  1173.   if (!args)
  1174.     error_no_arg ("process-id to attach");
  1175.  
  1176.   pid = strtol (args, &cptr, 0);
  1177.   if ((cptr == args) || (*cptr != '\0'))
  1178.     error ("Invalid process-id -- give a single number in decimal or 0xhex");
  1179.  
  1180.   if (from_tty)
  1181.       printf ("Attaching pid %s.\n", local_hex_string(pid));
  1182.  
  1183.   bzero ((char *)&ptrace_in,  sizeof (ptrace_in));
  1184.   bzero ((char *)&ptrace_out, sizeof (ptrace_out));
  1185.   ptrace_in.pid = pid;
  1186.  
  1187.   status = net_ptrace_clnt_call (PTRACE_ATTACH, &ptrace_in, &ptrace_out);
  1188.   if (status == -1)
  1189.     error (rpcerr);
  1190.   if (ptrace_out.status == -1)
  1191.     {
  1192.       errno = ptrace_out.errno;
  1193.       perror_with_name ("Attaching remote process");
  1194.     }
  1195.  
  1196.   /* It worked... */
  1197.   push_target (&vx_run_ops);
  1198.   inferior_pid = pid;
  1199.   vx_running = 0;
  1200.  
  1201. #if defined (START_INFERIOR_HOOK)
  1202.   START_INFERIOR_HOOK ();
  1203. #endif
  1204.  
  1205.   mark_breakpoints_out ();
  1206.  
  1207.   /* Set up the "saved terminal modes" of the inferior
  1208.      based on what modes we are starting it with.  */
  1209.   target_terminal_init ();
  1210.  
  1211.   /* Install inferior's terminal modes.  */
  1212.   target_terminal_inferior ();
  1213.  
  1214.   /* We will get a task spawn event immediately.  */
  1215.   init_wait_for_inferior ();
  1216.   clear_proceed_status ();
  1217.   stop_soon_quietly = 1;
  1218.   wait_for_inferior ();
  1219.   stop_soon_quietly = 0;
  1220.   normal_stop ();
  1221. }
  1222.  
  1223.  
  1224. /* detach_command --
  1225.    takes a program previously attached to and detaches it.
  1226.    The program resumes execution and will no longer stop
  1227.    on signals, etc.  We better not have left any breakpoints
  1228.    in the program or it'll die when it hits one.  For this
  1229.    to work, it may be necessary for the process to have been
  1230.    previously attached.  It *might* work if the program was
  1231.    started via the normal ptrace (PTRACE_TRACEME).  */
  1232.  
  1233. static void
  1234. vx_detach (args, from_tty)
  1235.      char *args;
  1236.      int from_tty;
  1237. {
  1238.   Rptrace ptrace_in;
  1239.   Ptrace_return ptrace_out;
  1240.   int signal = 0;
  1241.   int status;
  1242.  
  1243.   if (args)
  1244.     error ("Argument given to VxWorks \"detach\".");
  1245.  
  1246.   if (from_tty)
  1247.       printf ("Detaching pid %s.\n", local_hex_string(inferior_pid));
  1248.  
  1249.   if (args)        /* FIXME, should be possible to leave suspended */
  1250.     signal = atoi (args);
  1251.   
  1252.   bzero ((char *)&ptrace_in,  sizeof (ptrace_in));
  1253.   bzero ((char *)&ptrace_out, sizeof (ptrace_out));
  1254.   ptrace_in.pid = inferior_pid;
  1255.  
  1256.   status = net_ptrace_clnt_call (PTRACE_DETACH, &ptrace_in, &ptrace_out);
  1257.   if (status == -1)
  1258.     error (rpcerr);
  1259.   if (ptrace_out.status == -1)
  1260.     {
  1261.       errno = ptrace_out.errno;
  1262.       perror_with_name ("Detaching VxWorks process");
  1263.     }
  1264.  
  1265.   inferior_pid = 0;
  1266.   pop_target ();    /* go back to non-executing VxWorks connection */
  1267. }
  1268.  
  1269. /* vx_kill -- takes a running task and wipes it out.  */
  1270.  
  1271. static void
  1272. vx_kill (args, from_tty)
  1273.      char *args;
  1274.      int from_tty;
  1275. {
  1276.   Rptrace ptrace_in;
  1277.   Ptrace_return ptrace_out;
  1278.   int status;
  1279.  
  1280.   if (args)
  1281.     error ("Argument given to VxWorks \"kill\".");
  1282.  
  1283.   if (from_tty)
  1284.       printf ("Killing pid %s.\n", local_hex_string(inferior_pid));
  1285.  
  1286.   bzero ((char *)&ptrace_in,  sizeof (ptrace_in));
  1287.   bzero ((char *)&ptrace_out, sizeof (ptrace_out));
  1288.   ptrace_in.pid = inferior_pid;
  1289.  
  1290.   status = net_ptrace_clnt_call (PTRACE_KILL, &ptrace_in, &ptrace_out);
  1291.   if (status == -1)
  1292.     error (rpcerr);
  1293.   if (ptrace_out.status == -1)
  1294.     {
  1295.       errno = ptrace_out.errno;
  1296.       perror_with_name ("Killing VxWorks process");
  1297.     }
  1298.  
  1299.   /* If it gives good status, the process is *gone*, no events remain.  */
  1300.   inferior_pid = 0;
  1301.   pop_target ();    /* go back to non-executing VxWorks connection */
  1302. }
  1303.  
  1304. /* Clean up from the VxWorks process target as it goes away.  */
  1305.  
  1306. void
  1307. vx_proc_close (quitting)
  1308.      int quitting;
  1309. {
  1310.   inferior_pid = 0;        /* No longer have a process.  */
  1311.   if (vx_running)
  1312.     free (vx_running);
  1313.   vx_running = 0;
  1314. }
  1315.  
  1316. /* Cross-net conversion of floats to and from extended form.
  1317.    (This is needed because different target machines have different
  1318.     extended floating point formats.)  */
  1319.  
  1320. /* Convert from an extended float to a double.
  1321.  
  1322.    The extended float is stored as raw data pointed to by FROM.
  1323.    Return the converted value as raw data in the double pointed to by TO.
  1324. */
  1325.  
  1326. static void
  1327. vx_convert_to_virtual (regno, from, to)
  1328.     int regno;
  1329.     char *from;
  1330.     char *to;
  1331. {
  1332.   enum clnt_stat status;
  1333.  
  1334.   if (REGISTER_CONVERTIBLE (regno)) 
  1335.     {
  1336.       if (!target_has_fp) {
  1337.     *(double *)to = 0.0;    /* Skip the trouble if no float anyway */
  1338.     return;
  1339.       }
  1340.  
  1341.       status = net_clnt_call (VX_CONV_FROM_68881, xdr_ext_fp, from,
  1342.                   xdr_double, to);
  1343.  
  1344.       if (status == RPC_SUCCESS)
  1345.       return;
  1346.       else
  1347.       error (rpcerr);
  1348.     }
  1349.   else
  1350.     bcopy (from, to, REGISTER_VIRTUAL_SIZE (regno));
  1351. }
  1352.  
  1353.  
  1354. /* The converse:  convert from a double to an extended float.
  1355.  
  1356.    The double is stored as raw data pointed to by FROM.
  1357.    Return the converted value as raw data in the extended
  1358.    float pointed to by TO.
  1359. */
  1360.  
  1361. static void
  1362. vx_convert_from_virtual (regno, from, to)
  1363.     int regno;
  1364.     char *from;
  1365.     char *to;
  1366. {
  1367.   enum clnt_stat status;
  1368.  
  1369.   if (REGISTER_CONVERTIBLE (regno)) 
  1370.     {
  1371.       if (!target_has_fp) {
  1372.     bzero (to, REGISTER_RAW_SIZE (FP0_REGNUM));    /* Shrug */
  1373.     return;
  1374.       }
  1375.  
  1376.       status = net_clnt_call (VX_CONV_TO_68881, xdr_double, from,
  1377.                   xdr_ext_fp, to);
  1378.       if (status == RPC_SUCCESS)
  1379.       return;
  1380.       else
  1381.       error (rpcerr);
  1382.     }
  1383.   else
  1384.     bcopy (from, to, REGISTER_VIRTUAL_SIZE (regno));
  1385. }
  1386.  
  1387. /* Make an RPC call to the VxWorks target.
  1388.    Returns RPC status.  */
  1389.  
  1390. static enum clnt_stat
  1391. net_clnt_call (procNum, inProc, in, outProc, out)
  1392.     enum ptracereq procNum;
  1393.     xdrproc_t inProc;
  1394.     char *in;
  1395.     xdrproc_t outProc;
  1396.     char *out;
  1397. {
  1398.   enum clnt_stat status;
  1399.   
  1400.   status = clnt_call (pClient, procNum, inProc, in, outProc, out, rpcTimeout);
  1401.  
  1402.   if (status != RPC_SUCCESS)
  1403.       clnt_perrno (status);
  1404.  
  1405.   return status;
  1406. }
  1407.  
  1408. /* Clean up before losing control.  */
  1409.  
  1410. void
  1411. vx_close (quitting)
  1412.      int quitting;
  1413. {
  1414.   if (pClient)
  1415.     clnt_destroy (pClient);    /* The net connection */
  1416.   pClient = 0;
  1417.  
  1418.   if (vx_host)
  1419.     free (vx_host);        /* The hostname */
  1420.   vx_host = 0;
  1421. }
  1422.  
  1423. /* A vxprocess target should be started via "run" not "target".  */
  1424. /*ARGSUSED*/
  1425. static void
  1426. vx_proc_open (name, from_tty)
  1427.      char *name;
  1428.      int from_tty;
  1429. {
  1430.   error ("Use the \"run\" command to start a VxWorks process.");
  1431. }
  1432.  
  1433. /* Target ops structure for accessing memory and such over the net */
  1434.  
  1435. struct target_ops vx_ops = {
  1436.     "vxworks", "VxWorks target memory via RPC over TCP/IP",
  1437.     "Use VxWorks target memory.  \n\
  1438. Specify the name of the machine to connect to.",
  1439.     vx_open, vx_close, vx_attach, 0, /* vx_detach, */
  1440.     0, 0, /* resume, wait */
  1441.     0, 0, /* read_reg, write_reg */
  1442.     0, vx_convert_to_virtual, vx_convert_from_virtual,  /* prep_to_store, */
  1443.     vx_xfer_memory, vx_files_info,
  1444.     0, 0, /* insert_breakpoint, remove_breakpoint */
  1445.     0, 0, 0, 0, 0,    /* terminal stuff */
  1446.     0, /* vx_kill, */
  1447.     vx_load_command,
  1448.     0,  /* call_function */
  1449.     vx_lookup_symbol,
  1450.     vx_create_inferior, 0,  /* mourn_inferior */
  1451.     core_stratum, 0, /* next */
  1452.     1, 1, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  1453.     0, 0,            /* Section pointers */
  1454.     OPS_MAGIC,        /* Always the last thing */
  1455. };
  1456.  
  1457. /* Target ops structure for accessing VxWorks child processes over the net */
  1458.  
  1459. struct target_ops vx_run_ops = {
  1460.     "vxprocess", "VxWorks process",
  1461.     "VxWorks process, started by the \"run\" command.",
  1462.     vx_proc_open, vx_proc_close, 0, vx_detach, /* vx_attach */
  1463.     vx_resume, vx_wait,
  1464.     vx_read_register, vx_write_register,
  1465.     vx_prepare_to_store, vx_convert_to_virtual, vx_convert_from_virtual,
  1466.     vx_xfer_memory, vx_run_files_info,
  1467.     vx_insert_breakpoint, vx_remove_breakpoint,
  1468.     0, 0, 0, 0, 0,    /* terminal stuff */
  1469.     vx_kill,
  1470.     vx_load_command,
  1471.     call_function_by_hand,  /* FIXME, calling fns is maybe botched? */
  1472.     vx_lookup_symbol,
  1473.     0, vx_mourn_inferior,
  1474.     process_stratum, 0, /* next */
  1475.     0, 1, 1, 1, 1,    /* all mem, mem, stack, regs, exec */
  1476.             /* all_mem is off to avoid spurious msg in "i files" */
  1477.     0, 0,            /* Section pointers */
  1478.     OPS_MAGIC,        /* Always the last thing */
  1479. };
  1480. /* ==> Remember when reading at end of file, there are two "ops" structs here. */
  1481.  
  1482. void
  1483. _initialize_vx ()
  1484. {
  1485.   add_target (&vx_ops);
  1486.   add_target (&vx_run_ops);
  1487. }
  1488.